﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.TestItemForms
{
    public partial class TestItemAddForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.ToolBll toolBll = new BLL.ToolBll();
        private int _operat;
        TestItem _testItem;
        Action _InitGrid;
        public TestItemAddForm(Action InitGrid)   //新建构造函数
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _operat = 1;
            this.Text = ResourceOverall.CreateStr;
        }

        public TestItemAddForm(TestItem testItem,Action InitGrid)  //编辑构造函数
        {
            InitializeComponent();
            _operat = 2;
            this.Text = ResourceOverall.EditStr;
            InitEditModel(testItem);
            _testItem = testItem;
            _InitGrid=InitGrid;
        }

        private void TestItemAddForm_Load(object sender, EventArgs e)
        {
            if (_operat==1)
            {
                textEdit_order.Text=  (toolBll.GetMaxTestItemOrder() + 1).ToString();
            }
        }
        
        private void InitEditModel(TestItem testItem)
        {
            this.textEdit_ChineseName.Text = testItem.ChineseName;
            this.textEdit_EnglishName.Text = testItem.EnglishName;
            this.textEdit_Basis.Text = testItem.Basis;
            this.textEdit_Unit.Text = testItem.Unit;
            this.textEdit_Condition.Text = testItem.Condition;
            this.textEdit_Remark.Text = testItem.Remark;
            this.textEdit_order.Text = testItem.Order.ToString();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (_operat==1)
            {
                if (this.textEdit_ChineseName.Text.Trim() == "")
                {
                    XtraMessageBox.Show(ResourceOverall.InputNull);
                    this.textEdit_ChineseName.Focus();
                    return;
                }
                TestItem testItem = new TestItem();
                testItem.TestItemID = Guid.NewGuid().ToString();
                testItem.Basis = this.textEdit_Basis.Text;
                testItem.ChineseName = this.textEdit_ChineseName.Text;
                testItem.Condition = this.textEdit_Condition.Text;
                testItem.EnglishName = this.textEdit_EnglishName.Text;
                testItem.Remark = this.textEdit_Remark.Text;
                testItem.Unit = this.textEdit_Unit.Text;
                try
                {
                    testItem.Order = Convert.ToInt32( textEdit_order.Text);
                }
                catch (Exception ex)
                {
                    textEdit_order.Focus();
                    XtraMessageBox.Show(ex.Message);
                    return;
                }
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    db.TestItems.Add(testItem);
                    bool res = db.SaveChanges() > 0;
                    if (res)
                    {
                        _InitGrid();
                        this.Close();
                    }
                }
               
            }
            if (_operat==2)
            {
                using (OJCB4Entities db =new OJCB4Entities())
                {
                    TestItem testITem = db.TestItems.Find(_testItem.TestItemID);
                    testITem.Basis = this.textEdit_Basis.Text;
                    testITem.ChineseName = this.textEdit_ChineseName.Text;
                    testITem.Condition = this.textEdit_Condition.Text;
                    testITem.EnglishName = this.textEdit_EnglishName.Text;
                    testITem.Remark = this.textEdit_Remark.Text;
                    testITem.Unit = this.textEdit_Unit.Text;
                    try
                    {
                        testITem.Order = Convert.ToInt32(textEdit_order.Text);
                    }
                    catch (Exception ex)
                    {
                        textEdit_order.Focus();
                        XtraMessageBox.Show(ex.Message);
                        return;
                    }
                  
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        this.Close();
                    }
                }
            }
           

        }
    }
}